$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tcactivity/list?status=1',
        datatype: "json",
        colModel: [
            { label: '活动名称', name: 'activityName', index: 'ACTIVITY_NAME', width: 80 },
            { label: '活动最大人数', name: 'activityMaxNum', index: 'ACTIVITY_MAX_NUM', width: 80 },
            { label: '活动积分', name: 'activityIntegral', index: 'ACTIVITY_INTEGRAL', width: 80 },
            { label: '申请人', name: 'applicantId', index: 'APPLICANT_ID', width: 80 },
            { label: '联系人', name: 'contactsId', index: 'CONTACTS_ID', width: 80 },
            { label: '联系人电话', name: 'contactsPhone', index: 'CONTACTS_PHONE', width: 80 },
            { label: '活动地址', name: 'activityAddress', index: 'ACTIVITY_ADDRESS', width: 80 },
            { label: '开始时间', name: 'startTime', index: 'START_TIME', width: 80 },
            { label: '结束时间', name: 'endTime', index: 'END_TIME', width: 80 },
            { label: '活动状态', name: 'activityStatus', index: 'ACTIVITY_STATUS', width: 70,formatter: function(value, options, row){
                    if(value == "ACTIVITY_AUDIT_PASS"){
                    return '<span class="label label-success">申请审核通过</span>'
                    }else if(value == "ACTIVITY_SIGN_UP"){
                        return '<span class="label label-success">活动报名中</span>'
                    }else if(value == "ACTIVITY_IN_PROGRESS"){
                        return '<span class="label label-success">活动进行中</span>'
                    }else if(value == "ACTIVITY_CANCEL"){
                        return '<span class="label label-success">活动已取消</span>'
                    }else if(value == "ACTIVITY_FINISHED"){
                        return '<span class="label label-success">活动已结束</span>'
                    }else if(value == "ACTIVITY_APPRECIATED"){
                        return '<span class="label label-success">活动已打赏</span>'
                    }}},
        ],
        viewrecords: true,
        height: 580,
        rowNum: 50,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        tcActivity: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.tcActivity = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tcActivity.id == null ? "sys/tcactivity/save" : "sys/tcactivity/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tcActivity),
                    success: function(r){
                        if(r.code === 0){
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tcactivity/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        start:function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var status1 =$("#jqGrid tr[class*='success']").find("td:last span").text();
            var name =$("#jqGrid tr[class*='success']").find("td:eq(2)").text();
            var phone =$("#jqGrid tr[class*='success']").find("td:eq(7)").text();
            var applicantId =$("#jqGrid tr[class*='success']").find("td:eq(5)").text();
            if(status1=="活动进行中"){
                layer.alert("活动已经开始，无须再次开始");
            }else if(status1=="活动已结束"){
                layer.alert("活动已经结束，无须再次开始");
            }else{
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,closeBtn: 1
                    ,area: '400px;'
                    ,shade: 0.8
                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,btn: ['是', '否']
                    ,btnAlign: 'c'
                    ,moveType: 1 ,//拖拽模式，0或者1
                    content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">活动是否开始<br/>活动名称:    '+name+'<br/>活动申请人：     '+applicantId+'</div>'
                    ,
                    btn1: function (index){
                        var data = "status=ACTIVITY_IN_PROGRESS&id="+id+"&phone="+phone+"&smstext= “"+name+"” 已经开始！ 【领航社区】";
                        $.ajax({
                            type: "POST",
                            url: baseURL +"sys/tcactivity/option",
                            data: data,
                            dataType: "json",
                            success: function(result){
                                if(result.code == 0){
                                    layer.close(index);
                                    layer.alert('活动成功开始', function(index){
                                        location.reload();
                                    });
                                }else{
                                    layer.alert(result.msg);
                                }
                            }
                        });
                    }
                });
            }

        },
        end:function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var status1 =$("#jqGrid tr[class*='success']").find("td:last span").text();
            var name =$("#jqGrid tr[class*='success']").find("td:eq(2)").text();
            var phone =$("#jqGrid tr[class*='success']").find("td:eq(7)").text();
            var applicantId =$("#jqGrid tr[class*='success']").find("td:eq(5)").text();
            if(status1=="申请审核通过"){
                layer.alert("活动未开始，无须结束");
            }else if(status1=="活动已结束"){
                layer.alert("活动已经结束，无须再结束");
            }else{
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,closeBtn: 1
                    ,area: '400px;'
                    ,shade: 0.8
                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,btn: ['是', '否']
                    ,btnAlign: 'c'
                    ,moveType: 1 ,//拖拽模式，0或者1
                    content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">活动是否结束<br/>活动名称:    '+name+'<br/>活动申请人：     '+applicantId+'</div>'
                    ,
                    btn1: function (index){
                        var data = "status=ACTIVITY_FINISHED&id="+id+"&phone="+phone+"&smstext= “"+name+"” 已经结束！ 【领航社区】";
                        $.ajax({
                            type: "POST",
                            url: baseURL +"sys/tcactivity/option",
                            data: data,
                            dataType: "json",
                            success: function(result){
                                if(result.code == 0){
                                    layer.close(index);
                                    layer.alert('活动成功结束', function(index){
                                        location.reload();
                                    });
                                }else{
                                    layer.alert(result.msg);
                                }
                            }
                        });
                    }
                });
            }
        },
        getInfo: function(id){
            $.get(baseURL + "sys/tcactivity/info/"+id, function(r){
                vm.tcActivity = r.tcActivity;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});